package ngames.com.sushitv.temp.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;

import java.util.List;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.temp.Cache.BitmapCache;
import ngames.com.sushitv.temp.Class.Following;
import ngames.com.sushitv.view.MyCircleImageView;

/**
 * Created by edmond on 16-7-28.
 */
public class FollowingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<Following> followingArrayList;
    private LayoutInflater inflater;
    private AppApplication application;

    private static final int TYPE_FOOTER = 2;
    private static final int TYPE_ITEM = 1;

    public FollowingAdapter(Context context, List<Following> followingArrayList,AppApplication application){
        this.context = context;
        this.followingArrayList = followingArrayList;
        inflater = LayoutInflater.from(context);
        this.application = application;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if(viewType==TYPE_ITEM){
            View view = inflater.inflate(R.layout.item_following,parent, false);
            holder = new ItemHolder(view);
        }else if(viewType==TYPE_FOOTER){
            View view = inflater.inflate(R.layout.footer_following,parent,false);
            holder = new FooterHolder(view);
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position%2!=0){
            holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.single_bg));
        }else{
            holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.double_bg));
        }
        if(position!=getItemCount()-1){
            if(application.imageLoader==null){
                application.imageLoader = new ImageLoader(application.queue,new BitmapCache());
            }

            ((ItemHolder)holder).myCircleImageView.setErrorImageResId(R.mipmap.ic_launcher);
            ((ItemHolder)holder).myCircleImageView.setDefaultImageResId(R.mipmap.ic_launcher);
            ((ItemHolder)holder).myCircleImageView.setImageUrl(followingArrayList.get(position).getAvatar(),application.imageLoader);
            ((ItemHolder)holder).nameText.setText(followingArrayList.get(position).getUser_id());
            ((ItemHolder)holder).statusText.setText("statua");
            ((ItemHolder)holder).cover.setErrorImageResId(R.mipmap.ic_launcher);
            ((ItemHolder)holder).cover.setDefaultImageResId(R.mipmap.ic_launcher);
            ((ItemHolder)holder).cover.setImageUrl(followingArrayList.get(position).getCover(),application.imageLoader);
            ((ItemHolder)holder).titleText.setText(followingArrayList.get(position).getTitle());
            ((ItemHolder)holder).playCount.setText(followingArrayList.get(position).getPlay_count()+"");
            ((ItemHolder)holder).readCount.setText(followingArrayList.get(position).getDanmu_count()+"");
        }
    }

    @Override
    public int getItemCount() {
        return followingArrayList.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        if(position==getItemCount()-1){
            return TYPE_FOOTER;
        }else{
            return TYPE_ITEM;
        }
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        MyCircleImageView myCircleImageView;
        TextView nameText;
        TextView statusText;
        NetworkImageView cover;
        TextView titleText;
        TextView playCount;
        TextView readCount;
        public ItemHolder(View itemView) {
            super(itemView);
            myCircleImageView = (MyCircleImageView) itemView.findViewById(R.id.logo);
            nameText = (TextView) itemView.findViewById(R.id.nameText);
            statusText = (TextView) itemView.findViewById(R.id.statusText);
            cover = (NetworkImageView) itemView.findViewById(R.id.cover);
            titleText = (TextView) itemView.findViewById(R.id.titleText);
            playCount = (TextView) itemView.findViewById(R.id.playCountText);
            readCount = (TextView) itemView.findViewById(R.id.readCountText);
        }
    }

    class FooterHolder extends RecyclerView.ViewHolder{

        public FooterHolder(View itemView) {
            super(itemView);
        }
    }
}